package com.len.utils;

import com.itfsw.query.builder.SqlQueryBuilderFactory;
import com.itfsw.query.builder.support.builder.SqlBuilder;
import com.itfsw.query.builder.support.model.result.SqlQueryResult;
import org.apache.commons.lang3.StringUtils;

/**
 * @author songxiangyang
 * @date 2019/3/12
 */
public class Tools {
    public static String spilt(String str) {
        if(StringUtils.isNotBlank(str)) {
            StringBuffer sb = new StringBuffer();
            String[] temp = str.split(",");
            for (int i = 0; i < temp.length; i++) {
                if (!"".equals(temp[i]) && temp[i] != null) {
                    sb.append("'" + temp[i] + "',");
                }
            }
            String result = sb.toString();
            String tp = result.substring(result.length() - 1, result.length());
            if (",".equals(tp)) {
                return result.substring(0, result.length() - 1);
            } else {
                return result;
            }
        }
        return null;
    }

    public static String getQueryCondition(String dsl) {
        try {
            if(StringUtils.isNotBlank(dsl)) {
                SqlQueryBuilderFactory sqlQueryBuilderFactory = new SqlQueryBuilderFactory();
                SqlBuilder sqlBuilder = sqlQueryBuilderFactory.builder();
                String filterCondition = dsl.replace(".", "__");
                SqlQueryResult sqlQueryResult = sqlBuilder.build(filterCondition);
                String queryConditionPre = sqlQueryResult.getQuery(true);
                String queryCondition = queryConditionPre.replace("__", ".");
                return queryCondition;
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
